package com.zfjt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.OrderNumBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00021;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.service.OrderNumService;
import com.zfjt.utils.StringUtil;

/**
 * 确认订单流程走完接口
 * @author xk
 * @version 1.0
 * @date 2017年4月06日
 */
@Controller
public class AddImgIsAccomplishController {
	
	private ReturnBean mReturn;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	@Autowired
	private OrderNumService mOrderNum;
	
	private Map<String, String> fhdata; 
	
	private OrderNumBean dg;
	
	@RequestMapping(value="00021")
	@ResponseBody
	public ReturnBean AddImg12(HttpSession hseesion,HttpServletRequest request){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00021 num00020 = JSON.parseObject(data,PackNum00021.class); 
		ClientmessageBean mdata = mClientMessage.selectByPrimaryKey((short) Integer.parseInt(num00020.getId()));
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00018");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{	
			String is = "0";
			List<OrderNumBean> nData = mOrderNum.selectAll();
			for (int i = 0; i < nData.size(); i++) {
				OrderNumBean dg1 = nData.get(i);
				if (dg1.getId().equals(num00020.getOrdernum())) {
					dg = dg1;
					if (dg1.getType().equals("0")) {
						if (dg1.getAffiliationBrabch().equals(num00020.getBrabch())) {
							is = "2";     //序列号是该网点的能使用
						}else{
							is = "1";		//序列号不是该网点的不能使用
						}
					}else{
						is = "3";				//序列号已经使用
					}
				}		
			}
			System.out.println(is);
			if (is.equals("0")) {//数据库中无此序列号
				mReturn.setCode("002");
				fhdata.put("error", "ORDERNUM_ERROR");
				mReturn.setData(JSON.toJSONString(fhdata));
				mReturn.setPortnum("00021");
			}else if (is.equals("1")) {//该序列号不属于你所属网点
				mReturn.setCode("002");
				fhdata.put("error", "BRABCH_ERROR");
				mReturn.setData(JSON.toJSONString(fhdata));
				mReturn.setPortnum("00021");
			}else if (is.equals("2")) {
				//设置已经完成该订单
				mdata.setReject("1");;
				//设置订单状态，已申请
				mdata.setType("2");
				mClientMessage.updateByPrimaryKeySelective(mdata);
				//把该序列号状态设置成已使用
				dg.setType("1");
				mOrderNum.updateByPrimaryKeySelective(dg);
				mReturn.setCode("000");
				fhdata.put("succeed", "1");
				mReturn.setData(JSON.toJSONString(fhdata));
				mReturn.setPortnum("00021");
			}else if (is.equals("3")){//该序列号已经使用
				mReturn.setCode("002");
				fhdata.put("error", "APPLIED_ERROR");
				mReturn.setData(JSON.toJSONString(fhdata));
				mReturn.setPortnum("00021");
			}
		}
		return mReturn;
	}
}
